/**
 * 题目管理初始化
 */
var Subjects = {
    id: "SubjectsTable",//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    deptid:0
    
};

/**
 * 初始化表格的列
 */
Subjects.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '题目编号', field: 'subNo', visible: false, align: 'center', valign: 'middle'},
            {title: '外键', field: 'catagoryId', visible: false, align: 'center', valign: 'middle'},
            {title: '分类', field: 'caName', visible: true, align: 'center', valign: 'middle'},
            {title: '题目类别', field: 'examClass', visible: false, align: 'center', valign: 'middle'},
            {title: '题目内容', field: 'content', visible: true, align: 'center', width:'250px', valign: 'middle'},
            /*{title: '题目图片', field: 'examImageUrl', visible: true, align: 'center', valign: 'middle'},*/
            {title: '正确答案', field: 'answer', visible: true, align: 'center', valign: 'middle'},
            {title: '帮助', field: 'helps', visible: true, align: 'center', valign: 'middle'},
            /*{title: '帮助URL', field: 'helpsUrl', visible: false, align: 'center', valign: 'middle'},
            {title: '语音显示信息', field: 'sound', visible: true, align: 'center', valign: 'middle'},
            {title: '答题次数', field: 'times', visible: false, align: 'center', valign: 'middle'},*/
            {title: '创建时间', field: 'createTime', visible: false, align: 'center', valign: 'middle'},
            {title: '修改时间', field: 'updateTime', visible: false, align: 'center', valign: 'middle'},
            {title: '创建人', field: 'createUser', visible: false, align: 'center', valign: 'middle'},
            {title: '修改人', field: 'updateUser', visible: false, align: 'center', valign: 'middle'},
            {title: '备注', field: 'remark', visible: true, align: 'center', valign: 'middle'},
            {title: '语音讲解内容', field: 'voiceHelp', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Subjects.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Subjects.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加题目
 */
Subjects.openAddSubjects = function () {
    var index = layer.open({
        type: 2,
        title: '添加题目',
        area: ['800px', '420px'], // 宽高
        fix: false, // 不固定
        maxmin: true,
        content: Feng.ctxPath + '/subjects/subjects_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看题目详情
 */
Subjects.openSubjectsDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '题目详情',
            area: ['800px', '420px'], // 宽高
            fix: false, // 不固定
            maxmin: true,
            content: Feng.ctxPath + '/subjects/subjects_update/' + Subjects.seItem.id
        });
        this.layerIndex = index;
    }
};


/**
 * 打开查看题目详情
 */
Subjects.openSubjectsOption = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '题目答案选项',
            area: ['800px', '520px'], // 宽高
            fix: false, // 不固定
            maxmin: true,
            content: Feng.ctxPath + '/subjects/subjects_option/' + Subjects.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除题目
 */
Subjects.delete = function () {
    if (this.check()) {
    	 var operation = function(){
	    	var ajax = new $ax(Feng.ctxPath + "/subjects/delete", function (data) {
	            Feng.success("删除成功!");
	            //Subjects.table.refresh();
	            
	            Subjects.table.refresh(Subjects.queryString);
	            
	        }, function (data) {
	            Feng.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("subjectsId",Subjects.seItem.id);
	        ajax.start();
    	 };
    	 Feng.confirm("是否确认删除?",operation);
    }
};

/**
 * 语音内容生成声音
 */
Subjects.doAudio = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/subjects/doAudio", function (data) {
        	console.log(data);
        	if(data.code == 200){
                Feng.success("生成声音成功!");
                Subjects.table.refresh(Subjects.queryString);
        	}else{
        		 Feng.error("生成声音失败!");
        	}
            
        }, function (data) {
            Feng.error("生成声音失败!" + data.responseJSON.message + "!");
        });
        ajax.set("subjectsId",this.seItem.id);
        ajax.start();
    }
};


/**
 * 查询题目列表
 */
Subjects.search = function () {
	var queryData = {};
    queryData['condition'] = $("#condition").val();
    queryData['caNo'] = $("#caNo").val();
    Subjects.queryString = {query: queryData};
    Subjects.table.refresh({query: queryData});
};


/**
 * 显示父级菜单选择的树
 */
Subjects.showMenuSelectTree = function() {
	Feng.showInputTree("caName", "pcodeTreeDiv", 15, 34);
};

/**
 * 点击父级编号input框时
 */
Subjects.onClickDept = function(e, treeId, treeNode) {
	$("#caName").attr("value", Subjects.ztreeInstance.getSelectedVal());
	$("#caNo").attr("value", treeNode.id);
};


$(function () {

	var paramsId =[
	        	   /*{
	        	       "key":"classId",
	        	       "type":"select",
	        	       "id":"classId"
	        	   },
	        	   {
	        		   "key":"workId",
	        		   "type":"select",
	        		   "id":"workId"
	        	   }*/
	        	];
    var defaultColunms = Subjects.initColumn();
    var table = new BSTable("SubjectsTable", "/subjects/list", defaultColunms,paramsId);
    table.setPaginationType("service");
    Subjects.table = table.init();
    
    
	/*var ztree = new $ZTree("pcodeTree", "/subjects/selectMenuTreeList");
	ztree.bindOnClick(Subjects.onClickDept);
	ztree.init();
	Subjects.ztreeInstance = ztree;*/
});
